# Guess, Andrew and Alexander Coppock. Does Counter-Attitudinal Information Cause Backlash? Results from Three Large Survey Experiments. British Journal of Political Science, forthcoming.

# Produces Figure 1

rm(list = ls())

library(tidyverse)
library(estimatr)

load("GC_study_1.rdata")
load("GC_Study_2.rdata")
load("GC_Study_3.rdata")
source("GC_helpers.R")

# Study 1 -----------------------------------------------------------------

study_1 <-  study_1 %>% filter(!is.na(Z_information))

fit_1_1 <- lm_robust(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, data = filter(study_1, W1_Q1 == 1))
fit_1_2 <- lm_robust(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, data = filter(study_1, W1_Q1 == 0))
fit_1_3 <- lm_robust(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, data = filter(study_1, W1_Q1 == 1))
fit_1_4 <- lm_robust(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, data = filter(study_1, W1_Q1 == 0))


df_1_1 <-
  fit_1_1 %>% tidy %>% 
  mutate(dv = "Study 1: Composite Scale",
         study = "Gun Control",
         subset = "Proponents") %>%
  filter(term %in% c("Z_informationpro_information", "Z_informationcon_information"))
df_1_2 <-
  fit_1_2 %>% tidy %>% 
  mutate(dv = "Study 1: Composite Scale",
         study = "Gun Control",
         subset = "Opponents") %>%
  filter(term %in% c("Z_informationpro_information", "Z_informationcon_information"))
df_1_3 <-
  fit_1_3 %>% tidy %>% 
  mutate(dv = "Study 1: Support Gun Control",
         study = "Gun Control",
         subset = "Proponents") %>%
  filter(term %in% c("Z_informationpro_information", "Z_informationcon_information"))
df_1_4 <-
  fit_1_4 %>% tidy %>% 
  mutate(dv = "Study 1: Support Gun Control",
         study = "Gun Control",
         subset = "Opponents") %>%
  filter(term %in% c("Z_informationpro_information", "Z_informationcon_information"))


# Study 2 -----------------------------------------------------------------

fit_2_1 <- lm_robust(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode + age +  male + ideology + partyid + education + amount_T1,
            data= subset(study_2, initial_position=="pro_raise"))
fit_2_2 <- lm_robust(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode + age + male + ideology + partyid + education + amount_T1,
            data= subset(study_2, initial_position=="con_raise"))
fit_2_3 <- lm_robust(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode + age +  male + ideology + partyid + education + amount_T1,
             data= subset(study_2, initial_position=="pro_raise"))
fit_2_4 <- lm_robust(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode + age + male + ideology + partyid + education + amount_T1,
            data= subset(study_2, initial_position=="con_raise"))

df_2_1 <-
  fit_2_1 %>% tidy %>% 
  mutate(dv = "Study 2: T2 Amount",
         study = "Minimum Wage",
         subset = "Proponents") %>%
  filter(term %in% c("positive_information", "negative_information"))
df_2_2 <-
  fit_2_2 %>% tidy %>% 
  mutate(dv = "Study 2: T2 Amount",
         study = "Minimum Wage",
         subset = "Opponents") %>%
  filter(term %in% c("positive_information", "negative_information"))
df_2_3 <-
  fit_2_3 %>% tidy %>% 
  mutate(dv = "Study 2: T2 Favor",
         study = "Minimum Wage",
         subset = "Proponents") %>%
  filter(term %in% c("positive_information", "negative_information"))
df_2_4 <-
  fit_2_4 %>% tidy %>% 
  mutate(dv = "Study 2: T2 Favor",
         study = "Minimum Wage",
         subset = "Opponents") %>%
  filter(term %in% c("positive_information", "negative_information"))

# Study 3 -----------------------------------------------------------------

study_3 <-
  within(study_3,{
    positive_information <- info_content
    positive_information[info_content < 0] <- 0
    negative_information <- info_content
    negative_information[info_content > 0] <- 0
    negative_information <- negative_information* -1
  })

fit_3_1 <- lm_robust(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN") + support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race, data=subset(study_3, pro==1))
fit_3_2 <- lm_robust(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race, data=subset(study_3, pro==0))
fit_3_3 <- lm_robust(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race, data=subset(study_3, pro==1))
fit_3_4 <- lm_robust(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race, data=subset(study_3, pro==0))

df_3_1 <-
  fit_3_1 %>% tidy %>% 
  mutate(dv = "Study 3: T2 Attitude Toward Capital Punishment",
         study = "Capital Punishment",
         subset = "Proponents") %>%
  filter(term %in% c("positive_information", "negative_information"))
df_3_2 <-
  fit_3_2 %>% tidy %>% 
  mutate(dv = "Study 3: T2 Attitude Toward Capital Punishment",
         study = "Capital Punishment",
         subset = "Opponents") %>%
  filter(term %in% c("positive_information", "negative_information"))
df_3_3 <-
  fit_3_3 %>% tidy %>% 
  mutate(dv = "Study 3: T2 Belief in Deterrent Effect",
         study = "Capital Punishment",
         subset = "Proponents") %>%
  filter(term %in% c("positive_information", "negative_information"))
df_3_4 <-
  fit_3_4 %>% tidy %>% 
  mutate(dv = "Study 3: T2 Belief in Deterrent Effect",
         study = "Capital Punishment",
         subset = "Opponents") %>%
  filter(term %in% c("positive_information", "negative_information"))

# Synthesize --------------------------------------------------------------

df_1 <- bind_rows(df_1_1, df_1_2, df_1_3, df_1_4)
df_2 <- bind_rows(df_2_1, df_2_2, df_2_3, df_2_4)
df_3 <- bind_rows(df_3_1, df_3_2, df_3_3, df_3_4)



df <- bind_rows(df_1, df_2, df_3) 
  
df <- within(df, {
  term[term == "Z_informationpro_information"] <- "Positive Information"
  term[term == "Z_informationcon_information"] <- "Negative Information"
  term[term == "positive_information"] <- "Positive Information"
  term[term == "negative_information"] <- "Negative Information"
  term <- factor(term, levels = c("Positive Information", "Negative Information"))
  subset <- factor(subset, levels = c("Proponents", "Opponents"))
})

lims_df <- data.frame(
  dv = rep(c("Study 1: Composite Scale", "Study 1: Support Gun Control", "Study 2: T2 Amount", 
          "Study 2: T2 Favor", "Study 3: T2 Attitude Toward Capital Punishment","Study 3: T2 Belief in Deterrent Effect"), each = 2),
  term = "Positive Information",
  estimate = c(-.25, .25, -.15, .15, -2.2, 2.2, -1.4, 1.4, -.5, .5, -.7, .7),
  subset = "Proponents"
)

df %>%
  group_by(term) %>%
  summarise(sum(estimate >= 0))

df %>%
  summarise(sum(p.value < 0.05))

binom.test(23, n = 24)

g <- 
ggplot(df, aes(x = term, y = estimate, group = subset, shape = subset)) +
  geom_blank(data = lims_df) +
  geom_point(size = 2, position = position_dodge(width = .5)) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), 
                position = position_dodge(width = .5), width = 0, show.legend = TRUE) +
  facet_wrap(~dv, scales = "free", nrow = 3) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  ylab("Conditional Average Treatment Effect Estimates (Covariate Adjusted Models)")+
  theme_bw() +
  theme(strip.background = element_blank(),
        axis.title.x = element_blank(),
        legend.title = element_blank(),
        legend.key.width = unit(4, "lines"),
        legend.position = "bottom")
